<template>
  <div>
    <div style="background: #fff; width: 600px; margin: 60px auto; padding: 20px 50px;">

      <h1 style="text-align: center; margin: 30px auto">
        <i class="el-icon-user"></i>注册</h1>

      <!-- 登录表单 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input  v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="passwords">
          <el-input type="password" v-model="ruleForm.passwords"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {

    return {
      // 表单
      ruleForm: {
        username: '',
        password: '',
        passwords: ''
      },
      // 表单规则
      rules: {
        username: [
          {required: true,  message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true,  message: '请输入密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'}
        ],
        passwords: [
          {required: true,  message: '请确认密码', trigger: 'blur'},
          {min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = "http://localhost:9980/account/users/add-new";
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios.post(url, formData).then((response) => {
            let jsonResult = response.data;
            console.log(jsonResult);
            if (jsonResult.state == 20000) {
              this.$message({
                showClose: true,
                message: '添加用户成功！',
                type: 'success',
              });
              this.$router.push('/admin/login');
              this.resetForm(formName);
            } else {
              this.$message({
                showClose: true,
                message: jsonResult.message,
                type: 'error'
              });
            }
          });
        } else {
          console.log('error submit!!')
          return false;
        }
      });
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>

<style>
body {
  background: url("../assets/2.jpg") no-repeat;
  background-size: cover;
  position: absolute;
  top: 60px;
  bottom: 0;
  left: 0;
  right: 0;

}
</style>